

jQuery(function ($) {
    //验证码图片
    changeImgCode();
    function changeImgCode() {
        var img = document.getElementById('text_pic');
        img.src = host + "/api/code/img?phone=" + Math.floor(Math.random() * 100000000000);
    }
    $('#exchange').click(function () {
        changeImgCode();
    });
    //点击提交答案
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var user_id = user.id;
    var ask_id = window.location.href.split('-')[1].split('.')[0];
    $('#submit').click(function () {
        var content = $('textarea').val();
        var imgCode = $.trim($("#imgCode").val());
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        var create_time = year + '年' + month + '月' + day + '日 ' + hour + ':' + minute + ':' + second;
        var anon_flag = $("input[type='checkbox']").is(':checked');
        if (anon_flag == false) {
            anon_flag = 0;
        } else {
            anon_flag = 1;
        }
        var params = {
            'AskAnswer': {
                'ask_id': ask_id,
                'user_id': user_id,
                'anon_flag': anon_flag,
                'content': content,
                'create_time': create_time
            },
            'Tag': 'AskAnswer'
        };

        if (!user_id) {
            alert('您还没有登录，请登录后再操作！');
            return;
        } else if (!content) {
            alert('请输入您要提交的内容！');
            return;
        } else if (!imgCode) {
            alert('请输入验证码！');
            return;
        }
        send_ajax('post', '/get', params, function callback(result) {
            if (result.code == 200) {
                $('textarea').val('');
                $("#imgCode").val('');
                changeImgCode();
                alert('提交成功');
            } else {
                changeImgCode();
            }
        });
    });
    //获取最佳答案点赞数量

    $('#praise_best').click(function () {
        var best_id = $('.best_answer_detail').attr('data-id');
        var params = {
            "Like": {
                "type": 1,
                "holder_id": best_id
            }
        };
        send_ajax('post', '/head', params, function callback(result) {
            if (result.code == 200) {}

            // var count = result.Like.count;
            // if (result.Like.count) {
            //     $('.best_praise span').html(count);
            //     $('.best_praise img').attr("src", '/asset/image/thum_up.png')
            // } else {
            //     $('.best_praise img').attr("src", '/asset/image/thum_up2.png')
            // }

            // if ($('.best_praise img').attr('src') == '/asset/image/thum_up2.png') {
            //     $('.best_praise img').click(function () {
            //         $('.best_praise img').attr("src", '/asset/image/thum_up.png')
            //         $('.best_praise span').html(count + 1);
            //     })
            // }
        });
    });

    //获取其他答案点赞数量

    $('.praise_other').click(function () {
        var other_id = $(this).parents('.other_answer_detail').attr('data-id');
        var params = {
            "Like": {
                "type": 1,
                "holder_id": other_id
            }
        };
        send_ajax('post', '/head', params, function callback(result) {
            if (result.code == 200) {
            }

            // var count = result.Like.count;
            // if (result.Like.count) {
            //     $('.other_praise span').html(count);
            //     $('.other_praise img').attr("src", '/asset/image/thum_up.png')
            // } else {
            //     $('.other_praise img').attr("src", '/asset/image/thum_up2.png')
            // }

            // if ($('.other_praise img').attr('src') == '/asset/image/thum_up2.png') {
            //     $('.other_praise img').click(function () {
            //         $('.other_praise img').attr("src", '/asset/image/thum_up.png')
            //         $('.other_praise span').html(count + 1);
            //     })
            // }
        });
    });
    var clickparams = {
        "Ask": {
            "id": ask_id,
            "clicks+": 1
        }
    };
    send_ajax('post', '/put', clickparams, function callback(res) {});
});